package com.logos.cartoon.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.logos.cartoon.entity.AuthUser;
import com.logos.cartoon.req.AuthUserReq;
import com.logos.cartoon.resp.AuthUserResp;
import com.logos.cartoon.resp.CharacterInfoResp;
import com.logos.cartoon.result.Result;
import com.logos.cartoon.service.AuthUserService;
import com.logos.cartoon.utils.LoginContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户表(AuthUser)表控制层
 *
 * @author rose
 * @since 2025-03-17 20:06:04
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class AuthUserController {
    /**
     * 服务对象
     */
    @Resource
    private AuthUserService authUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<AuthUser> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.authUserService.queryById(id));
    }


    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody AuthUserReq authUserReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserController.java.login.authUserRequest:{}", JSON.toJSONString(authUserReq));
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(authUserReq.getPhone()), "手机号不能为空");
            Preconditions.checkNotNull(authUserReq.getPassword(), "密码不能为空");
            String token = authUserService.login(authUserReq);
            return Result.ok(token);
        } catch (Exception e) {
            log.error("AuthUserController.login.error:{}", e.getMessage(), e);
            return Result.fail("登录失败");
        }
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody AuthUserReq authUserReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserController.java.register.authUserRequest:{}", JSON.toJSONString(authUserReq));
            }
            Preconditions.checkNotNull(authUserReq.getNickName(), "昵称不能为空");
            Preconditions.checkArgument(StringUtils.isNotBlank(authUserReq.getPhone()), "手机号不能为空");
            Preconditions.checkNotNull(authUserReq.getPassword(), "密码不能为空");
            String token = authUserService.register(authUserReq);
            return Result.ok(token);
        } catch (Exception e) {
            log.error("AuthUserController.register.error:{}", e.getMessage(), e);
            return Result.fail("注册失败");
        }
    }

    /**
     * 获取个人信息
     */
    @GetMapping("/info")
    public Result<AuthUser> getUserInfo() {
        try {
            String loginPhone = LoginContextHolder.getLoginPhone();
            log.info("AuthUserController.getUserInfo.loginPhone:{}", loginPhone);
            AuthUser authUser = authUserService.getUserInfo(loginPhone);
            return Result.ok(authUser);
        } catch (Exception e) {
            log.error("AuthUserController.getUserInfo.error:{}", e.getMessage(), e);
            return Result.fail("获取个人信息失败");
        }
    }

    /**
     * 修改个人信息
     */
    @PutMapping("/info")
    public Result<Boolean> update(@RequestBody AuthUserReq authUserReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthUserController.java.register.authUserRequest:{}", JSON.toJSONString(authUserReq));
            }
            Preconditions.checkNotNull(authUserReq.getNickName(), "昵称不能为空");
            Preconditions.checkNotNull(authUserReq.getSex(), "性别不能为空");
            String loginPhone = LoginContextHolder.getLoginPhone();
            authUserReq.setPhone(loginPhone);
            Boolean ret = authUserService.update(authUserReq);
            return Result.ok(ret);
        }catch (Exception e) {
            log.error("AuthUserController.update.error:{}", e.getMessage(), e);
            return Result.fail("修改个人信息失败");
        }
    }

    /**
     * 查询我的
     */
    @GetMapping("/my")
    public Result<AuthUserResp> queryMy() {
        try {
            String loginPhone = LoginContextHolder.getLoginPhone();
            log.info("AuthUserController.getUserInfo.loginPhone:{}", loginPhone);
            AuthUserResp authUserResp = authUserService.queryMy(loginPhone);
            return Result.ok(authUserResp);
        }catch (Exception e) {
            log.error("AuthUserController.queryMy.error:{}", e.getMessage(), e);
            return Result.fail("查询我的失败");
        }
    }

    /**
     * 查询当前用户点赞列表
     */
    @GetMapping("/like")
    public Result<List<CharacterInfoResp>> queryMyLike() {
        try {
            String loginPhone = LoginContextHolder.getLoginPhone();
            log.info("AuthUserController.queryMyLike.loginPhone:{}", loginPhone);
            List<CharacterInfoResp> result = authUserService.queryMyLike(loginPhone);
            return Result.ok(result);
        }catch (Exception e) {
            log.error("AuthUserController.queryMyLike.error:{}", e.getMessage(), e);
            return Result.fail("查询我的失败");
        }
    }

}

